import { tajax } from '../js/tajax'

export function newShop(contact, name, desc, loc, lng, lat, licenseId, startPrice, shipPrice) {
    tajax({
        url: '/business/shop/new',
        method: 'post',
        data: {
            contact: contact,
            name: name,
            desc: desc,
            loc: loc,
            lng: lng,
            lat: lat,
            licenseId: licenseId,
            startPrice: startPrice,
            shipPrice: shipPrice
        },
        success: function (data) {
            alert(data.data)
            window.location.href = '/business/shops'
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function listShops(result) {
    tajax({
        url: '/business/shop/list',
        method: 'get',
        success: function (data) {
            if (data.code == 0) {
                result(data.data)
            } else {
                result([])
            }
        },
        error: function (data) {
            result([])
        }
    });
}

export function openShop(shopId) {
    tajax({
        url: '/business/shop/open',
        method: 'post',
        data: {
            shopId: shopId
        },
        success: function (data) {
            alert(data.data)
            window.location.href = window.location.href
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function closeShop(shopId) {
    tajax({
        url: '/business/shop/close',
        method: 'post',
        data: {
            shopId: shopId
        },
        success: function (data) {
            alert(data.data)
            window.location.href = window.location.href
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function deleteShop(shopId) {
    tajax({
        url: '/business/shop/delete',
        method: 'post',
        data: {
            shopId: shopId
        },
        success: function (data) {
            alert(data.data)
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function editShop(id, contact, startPrice, shipPrice, desc) {
    tajax({
        url: '/business/shop/edit',
        method: 'post',
        data: {
            id: id,
            contact: contact,
            shipPrice: shipPrice,
            startPrice: startPrice,
            desc: desc
        },
        success: function (data) {
            alert(data.data)
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function getShop(id, result) {
    tajax({
        url: '/business/shop/get',
        method: 'get',
        data: {
            id: id
        },
        success: function (data) {
            if (data.code == 0) {
                result(data.data)
            } else {
                result({})
            }
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function blockAdd(shopId, name) {
    tajax({
        url: '/business/block/add',
        method: 'post',
        data: {
            shopId: shopId,
            name: name
        },
        success: function (data) {
            alert(data.data)
            window.location.reload()
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function blockRemove(blockId) {
    tajax({
        url: '/business/block/remove',
        method: 'post',
        data: {
            blockId: blockId
        },
        success: function (data) {
            alert(data.data)
            window.location.reload()
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function itemGet(id, result) {
    tajax({
        url: '/business/item/get',
        method: 'post',
        data: {
            id: id
        },
        success: function (data) {
            if (data.code == 0) {
                result(data.data);
            }
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function itemAdd(comm) {
    tajax({
        url: '/business/item/add',
        method: 'post',
        data: {
            blockId: comm.blockId,
            name: comm.name,
            image: comm.image,
            description: comm.description,
            specifications: comm.specifications,
            price: comm.price,
            minQuantity: comm.minQuantity,
            packPrice: comm.packPrice
        },
        success: function (data) {
            alert(data.data)
            window.location.reload()
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function itemEdit(comm) {
    tajax({
        url: '/business/item/edit',
        method: 'post',
        data: {
            itemId: comm.id,
            blockId: comm.blockId,
            name: comm.name,
            image: comm.image,
            description: comm.description,
            specifications: comm.specifications,
            price: comm.price,
            minQuantity: comm.minQuantity,
            packPrice: comm.packPrice
        },
        success: function (data) {
            alert(data.data)
            window.location.reload()
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function itemRemove(itemId) {
    tajax({
        url: '/business/item/remove',
        method: 'post',
        data: {
            itemId: itemId
        },
        success: function (data) {
            alert(data.data)
            window.location.reload()
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function acceptOrder(orderId) {
    tajax({
        url: '/business/order/accept',
        method: 'post',
        data: {
            orderId: orderId
        },
        success: function (data) {
            alert(data.data)
            window.location.reload()
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function getOrder(orderId) {
    tajax({
        url: '/business/order/get',
        method: 'get',
        data: {
            orderId: orderId
        },
        success: function (data) {
            alert(data.data)
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function allOrders(result) {
    tajax({
        url: '/business/order/all',
        method: 'get',
        success: function (data) {
            if (data.code === 0) {
                result(data.data)
            } else {
                result([])
            }
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function getShopOrders(shopId, result) {
    tajax({
        url: '/business/order/shop',
        data: {
            shopId: shopId
        },
        method: 'get',
        success: function (data) {
            if (data.code == 0) {
                result(data.data)
            } else {
                result([])
            }
        },
        error: function (data) {
            alert(data.message)
        }
    });
}